﻿

Imports System.Data
Imports VSModel
Imports Cls_Common
Partial Class Control_sysFunction_sysFunctionMenu
    Inherits System.Web.UI.UserControl
    Public strMenu As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sufixURL = Request.Url.ToString.Split("Page")(0)
        Dim PureURL = Request.Url.ToString.Replace(sufixURL, "")
        If Session("Function") Is Nothing Then
            Response.Redirect("~/Login.aspx")

        Else
            'check more permission here
            Dim arrFunctionId As Integer() = Session("Function")
            ' neu la page/logged va insufficient value thi ko check quyen
            If PureURL <> "Page/Logged.aspx" AndAlso PureURL <> "Page/NonPrivilege.aspx" Then
                Dim URL2Check As String = PureURL.Substring(0, PureURL.LastIndexOf("/")) & "/List.aspx"
                Using _data As New VSEntities
                    Dim p = (From q In _data.SysFunctions Where arrFunctionId.Contains(q.FunctionId) And q.URL = URL2Check Select q).FirstOrDefault
                    If p Is Nothing Then
                        ''Response.Redirect("~/Page/NonPrivilege.aspx")
                    End If
                End Using
            End If


        End If
        strMenu = CreateMenu()

    End Sub

    Private Function CreateMenu() As String

        Dim i As Integer = 1
        Dim sMenu As String = ""
        Dim sufixURL = Request.Url.ToString.Split("Page")(0)
        Dim arrFunctionId As Integer() = Session("Function")

        ' kiem tra neu la Bien tap 'filter lai nhom theo Contents Or Not
        Dim lstRole As Integer() = Session("RoleID")
        'Using _data As New VSEntities
        '    Dim LstRoles As Integer() = (From q In _data.SysFuncRolesStatusPermissions Where lstRole.Contains(q.RoleId) And q.FunctionId = Function_Name.Content Select q.RoleId).ToArray
        '    If LstRoles.Length > 0 Then

        '        sMenu = "<div class='BienTapBaiViet'><a href='" & MainDomain & "Page/Contents/List.aspx" & "'>Bài viết</a>" & " | <a href='" & MainDomain & "Page/Contents/ToChucNhom.aspx" & "'>Tổ chức nhóm</a>" & " | <a href='" & MainDomain & "Page/Contents/ThongKeBaiViet.aspx" & "'>Thống kê Bài viết</a>" & " | <a href='" & MainDomain & "Page/Contents/NhuanBut.aspx" & "'>Nhuận bút</a>"

        '        If arrDuyetCongTacVien.Contains(Session("UserType")) Then
        '            sMenu = sMenu & "| <a href='" & MainDomain & "Page/Contents/CongTacVien.aspx" & "'>Cộng tác viên</a>"
        '        End If
        '        sMenu = sMenu & "</div>"
        '        Return sMenu
        '    End If
        'End Using
        ' end kiem tra


        Using data As New VSEntities
            'get first menu
            'Dim p = From q In data.Functions Where q.ParentId = 0 And q.IsMenu = True Order By q.Sort Ascending Select q
            Dim p = (From q In data.SysFunctions Where q.ParentId = 0 And q.HrefName <> "" _
                                                        And q.IsMenu = True _
                                                        And arrFunctionId.Contains(q.FunctionId) _
                                                       Order By q.Sort Ascending Select q).ToList

            If Not p Is Nothing Then
                For Each a In p
                    'neu menu dang duoc open
                    If CheckOpenItem(a) Then
                        sMenu = sMenu & "<ul class=""current"" id=""MenuCurrent"">" & vbCrLf
                        sMenu = sMenu & "<div>" & vbCrLf
                        sMenu = sMenu + "<li>" & "<a href=""javascript:return false;""><span>" & a.HrefName & "</span></a>" & vbCrLf
                        ' Draw subMenu Here
                        Dim sSubMenu As String = ""
                        sSubMenu = SubMenu(a.FunctionId, i, "show")
                        sMenu = sMenu & sSubMenu
                        sMenu = sMenu & "</li>" & vbCrLf
                        sMenu = sMenu & "</div>" & vbCrLf
                        sMenu = sMenu & "</ul>" & vbCrLf
                        'nguoc lai 
                    Else
                        sMenu = sMenu & "<ul class=""select Nolink"" id=""MenuSelect"">" & vbCrLf
                        sMenu = sMenu & "<div>" & vbCrLf

                        sMenu = sMenu + "<li>" & "<a href=""javascript:return false;""><span>" & a.HrefName & "</span></a>" & vbCrLf
                        ' Draw subMenu Here
                        Dim sSubMenu As String = ""
                        sSubMenu = SubMenu(a.FunctionId, i, "select_sub")
                        sMenu = sMenu & sSubMenu
                        sMenu = sMenu & "</li>" & vbCrLf
                        sMenu = sMenu & "</div>" & vbCrLf
                        sMenu = sMenu & "</ul>" & vbCrLf
                    End If
                    i = i + 1
                Next
            End If
        End Using

        Return sMenu

    End Function

    Private Function SubMenu(ByVal FunctionId As Integer, ByVal padding As Integer, ByVal classOpen As String) As String
        Dim strDes As String = ""
        Dim sufixURL = Request.Url.ToString.Split("Page")(0)
        Dim arrFunctionId As Integer() = Session("Function")
        Using data As New VSEntities
            'get first menu
            Dim p As List(Of SysFunction) = (From q In data.SysFunctions Where q.ParentId = FunctionId And q.IsMenu = True And arrFunctionId.Contains(q.FunctionId) Order By q.Sort Ascending Select q).ToList

            '    ' Get ChildNode value for each node
            strDes = strDes & "<div class=""" & classOpen & """ id=""sub_id"">" & vbCrLf
            strDes = strDes & "<ul class=""sub_" & padding & """>" & vbCrLf

            If Not p Is Nothing Then
                For Each a As SysFunction In p
                    'Take ID querystring in other to set active
                    If CheckOpenItem(a) Then
                        strDes = strDes + "<li class='ActiveSubMenu'>" & "<a href=" & sufixURL & a.URL & ">" & a.HrefName & "</a></li>" & vbCrLf
                    Else
                        strDes = strDes + "<li class='NormalSubMenu'>" & "<a href=" & sufixURL & a.URL & ">" & a.HrefName & "</a></li>" & vbCrLf
                    End If
                Next
            End If

            strDes = strDes & "</ul>" & vbCrLf
            strDes = strDes & "</div>" & vbCrLf

        End Using
        Return strDes

    End Function

    Public Function CheckOpenItem(ByVal a As SysFunction) As String

        Dim sufixURL = Request.Url.ToString.Split("Page")(0)
        Dim PureURL = Request.Url.ToString.Replace(sufixURL, "")
        'neu la report thi active khac
        If PureURL.Contains("Page/Logged.aspx") Then
            '' Nếu là Tra cứu thì active khác
            'If Not PureURL.Contains("Page/TraCuu/") Then
            '    PureURL = PureURL.Substring(0, PureURL.LastIndexOf("/") + 1)
            'Else
            '    PureURL = PureURL.Replace("Page/TraCuu/", "")
            'End If
            Return 0

            'PureURL = PureURL.Replace("Page/Report/", "")
        End If


        If Not a.URL Is Nothing Then
            ' PureURL = a.URL.Replace("List.aspx", "")
            If a.URL.Contains(PureURL) Then
                Return 1
            End If

        End If

        Using data As New VSEntities
            'lay ra FunctionId cua cur Menu
            Dim p = (From q In data.SysFunctions Where q.URL.Contains(PureURL) Select q).FirstOrDefault
            If Not p Is Nothing Then
                Dim ParentId = p.ParentId
                'lay menu cha
                Dim k = (From q In data.SysFunctions Where q.FunctionId = ParentId Select q).FirstOrDefault
                If Not k Is Nothing Then
                    'kiem tra neu = FuncionId truyen vao
                    If k.FunctionId = a.FunctionId Then
                        Return 1
                    End If
                End If
            End If
        End Using

        Return 0


    End Function


End Class


'Imports System.Data
'Imports VSModel
'Imports Cls_Common
'Partial Class Control_sysFunction_sysFunctionMenu
'    Inherits System.Web.UI.UserControl
'    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'        If Not Page.IsPostBack Then
'            BindMenu()
'        End If
'    End Sub
'    Protected Sub BindMenu()
'        Using data As New VSEntities
'            If Session("Function") Is Nothing Then
'                Response.Redirect("~/Login.aspx")
'            End If
'            'Dim arrFuntionId As Integer() = Session("Function")

'            Dim p = From q In data.SysFunctions Where q.IsMenu = True Order By q.FunctionName Ascending Select q
'            'check role here

'            With rptMenu
'                .DataSource = p
'                .DataBind()
'            End With


'        End Using

'    End Sub

'    Protected Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
'        Dim hplTitle As HyperLink
'        hplTitle = e.Item.FindControl("hplTitle")
'        Dim sufixURL = Request.Url.ToString.Split("Page")(0)

'        Dim data As New SysFunction
'        Dim p = CType(e.Item.DataItem, SysFunction)
'        hplTitle.NavigateUrl = MainDomain & p.URL
'        If Request.Url.ToString.Contains(p.URL.Replace("List.aspx", "")) Then
'            hplTitle.Text = "<span class=""current"">" & p.FunctionName & "</span>"
'            hplTitle.CssClass = "current"
'        Else
'            hplTitle.Text = "<span>" & p.FunctionName & "</span>"
'        End If

'    End Sub




'End Class
